import {deepCopy} from "@/utils";
import {getOrderAccountList} from "@/api/order";

export default {
  data(){
    return {
      userAccountList : [],
      userAccountGroup1: [], // 多店铺的用户
      userAccountGroup2: [], // 单店铺的用户
    };
  },
  props: {
    params: {
      type: Object,
      default: function(){
        return {};
      }
    },
    // 过滤无效用户
    filterStop: {
      type: Boolean,
      default: false,
    }
  },
  async mounted() {
    await this.getUser()
  },
  methods:{
    async getUser(){
      await getOrderAccountList(this.params).then(res=>{
        this.userAccountList = [];

        const userIds = [];

        res.data.forEach(item => {
          if(this.filterStop && item.status !== 1){
            return;
          }
          if(this.filterStop && item.accountNum === 0){
            return;
          }
          const obj = deepCopy(item)
          obj.check = true;

          this.userAccountList.push(obj)
          userIds.push(item.id)
        })

        this.userAccountGroup1 = [];
        this.userAccountGroup2 = [];
        this.userAccountList.forEach(item=>{
          if(item.accountList.length > 1){
            const obj = {
              id: item.id,
              account: item.account,
              indeterminate: false,
              check: true,
              num: item.num,
              accountList: [],
            }
            item.accountList.forEach(account => {
              const obj2 = deepCopy(account)
              obj2.check = true;
              obj.accountList.push(obj2)
            })
            this.userAccountGroup1.push(obj)
          }else{
            const obj = {
              id: item.id,
              account: item.account,
              check: true,
              num: item.num,
              nickname: item.accountList[0] ? item.accountList[0].nickname : '',
              showName: item.accountList[0] ? item.accountList[0].showName : '',
              accountId: item.accountList[0] ? item.accountList[0].accountId : 0,
            }
            this.userAccountGroup2.push(obj)
          }
        })

        this.$emit('input',userIds)
        this.$emit('apiUserFinish',true);
      })
    },
    getAgain(){
      getOrderAccountList(this.params).then(res=>{
        const data = res.data;
        data.forEach(item=>{
          this.userAccountList.some(_user=>{
            if(_user.id === item.id){
              _user.num = item.num
              return true;
            }
          })
          if(item.accountList.length > 1){
            this.userAccountGroup1.some(group1=>{
              if(group1.id === item.id){
                group1.num = item.num
                item.accountList.forEach(account=>{
                  group1.accountList.some(groupAccount=>{
                    if(groupAccount.id === account.id){
                      groupAccount.num = account.num
                      return true
                    }
                  })
                })

                return true;
              }
            })
          }else{
            this.userAccountGroup2.some(group2=>{
              if(group2.id === item.id){
                group2.num = item.num
                return true;
              }
            })
          }

        })

      })
    }
  }
}
